﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using BoutiquePOS.DAL.DAO;

namespace BoutiquePOS.DAL.Gateway
{
    public class RackGateway : DbGateway
    {
        public string SaveRack(Rack aRack)
        {
            try
            {
                ConnectionObj.Open();
                string query = String.Format("INSERT INTO PosRack VALUES( @rackName)");
                CommandObj.Parameters.Clear();
                CommandObj.CommandText = query;
                CommandObj.Parameters.Add("@rackName", SqlDbType.VarChar);
                CommandObj.Parameters["@rackName"].Value = aRack.RackName;
                CommandObj.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("Company couldn't saved", ex);
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }
            }
            return "Save successfully";
        }


        public List<Rack> GetAllRack()
        {
            List<Rack> racks = new List<Rack>();
            try
            {
                ConnectionObj.Open();
                string query = String.Format("SELECT * FROM PosRack");

                CommandObj.CommandText = query;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    Rack aRack = new Rack();
                    aRack.RackId = Convert.ToInt32(reader["RackId"]);
                    aRack.RackName = reader["RackName"].ToString();
                    racks.Add(aRack);
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Error occurred during Form loading from your system.", exception);
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }
            }
            return racks;
        }

        public string DeleteRackById(int rackId)
        {
            int i = 0;
            try
            {
                ConnectionObj.Open();
                string delete = String.Format("DELETE FROM PosRack WHERE rackId='{0}'", rackId);
                CommandObj.CommandText = delete;
                i = CommandObj.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw new Exception("Database Error");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }
            }
            if (i != 0)
            {
                return "Delete Successfully";
            }
            else
            {
                return "Rack Id does not found";
            }
        }

        public List<Rack> GetAllRackByCodeOrName(int i, string rackId)
        {
            List<Rack> racks = new List<Rack>();
            string select = "";

            try
            {
                ConnectionObj.Open();
                if (i == 0)
                {

                    select = String.Format("SELECT * FROM PosRack WHERE rackId ='{0}'", Convert.ToInt32(rackId));
                }
                if (i == 1)
                {
                    rackId = rackId + "%";
                    select = String.Format("SELECT * FROM PosRack WHERE rackName LIKE '" + rackId + "'");
                }

                CommandObj.CommandText = select;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    Rack rack = new Rack();
                    rack.RackName = reader[1].ToString();
                    rack.RackId = Convert.ToInt32(reader[0]);
                    racks.Add(rack);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }
            }
            return racks;
        }

    }
}